<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class CommentsController extends BaseController
{
    protected $api_url = [
        'show'=>['get','buyer/products/{product_id}/comments/sellerComment/%d'],
        'show_order' => ['get', 'buyer/orders/%d'],
        'store' => ['post', 'buyer/products/%d/comments'],
        'index_order_products' => ['get', 'buyer/orders/%d/order_products']
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function index()
    {

    }


    public function show($order_id, $order_code)
    {
        //请求部分
        $user_comment_ret = http_web($this->api_url['show'], [$order_code])->auto(false)->send();

        //请求部分-买家评价
        $order = http_web($this->api_url['show_order'], [$order_id])->send(['with' => 'orderProductComment']);

        return view('web.user.seller.common.comment', ['order_id' => $order_id, 'user_comment' => $user_comment_ret['data'],'order_product_comment'=>$order['data']['order_product_comment']]);
    }

    public function edit()
    {

    }

    public function create($order_id)
    {
        //参数部分
        $params = [
            'search' => search_params(['is_appraise' => 0]),
            'with' => 'product;sku',
        ];

        //请求部分
        $ret = http_web($this->api_url['index_order_products'], [$order_id])->send($params);
        return view('web.user.buyer.comment', ['order_products' => $ret['data'], 'order_id' => $order_id]);
    }

    public function store($order_id)
    {
        $order_products = $this->request->get('order_product', []);
//        dd($order_products);
        foreach ($order_products as $order_product_id => $order_product) {
            $overall = array_get($order_product, 'overall');
            $product_comment = array_get($order_product, 'product_comment');
            $seller_comment = array_get($order_product, 'seller_comment');
            $logistics_comment = array_get($order_product, 'logistics_comment');
            $comment = array_get($order_product, 'comment');
            $product_id = array_get($order_product, 'product_id');
            if ($overall && $product_comment && $seller_comment && $logistics_comment) {
                $ret = http_web($this->api_url['store'], [$product_id])->send(compact('order_product_id', 'overall', 'product_comment', 'seller_comment', 'logistics_comment', 'comment'));

            }
        }
        if($ret){
            return redirect('/member/buyer/orders');
        }else{
            return redirect()->route('orders.comments.create', $order_id);
        }
    }

    public function update($id)
    {

    }
}